package ceph

import (
	"filestore-rebuild/config"
	"gopkg.in/amz.v1/aws"
	"gopkg.in/amz.v1/s3"
)

var cephConn *s3.S3

// GetCephConnection 获取ceph连接
func GetCephConnection() *s3.S3 {
	if cephConn != nil {
		return cephConn
	}
	// 1.初始化ceph的一些信息
	auth := aws.Auth{
		AccessKey: config.File.MustValue("ceph", "accessKey"),
		SecretKey: config.File.MustValue("ceph", "secretKey"),
	}
	region := aws.Region{
		Name:                 "default",
		EC2Endpoint:          config.File.MustValue("ceph", "endpoint"),
		S3Endpoint:           config.File.MustValue("ceph", "endpoint"),
		S3BucketEndpoint:     "",
		S3LocationConstraint: false,
		S3LowercaseBucket:    false,
		Sign:                 aws.SignV2,
	}
	// 2.创建s3类型的连接
	return s3.New(auth, region)
}

// GetCephBucket 获取指定的bucket对象
func GetCephBucket(bucket string) *s3.Bucket {
	conn := GetCephConnection()
	return conn.Bucket(bucket)
}
